/**
 * 2016年9月8日
 */
package assignments;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Properties;
import java.util.function.BiFunction;
import java.util.function.Consumer;

import com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory.Default;

import assignments.RunClassifier.Counter;
import weka.classifiers.Classifier;
import weka.core.Instances;
import weka.core.converters.CSVSaver;

/**
 * @author Alex
 *
 */
public class Util {
	
	protected static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
	protected static File dir = new File("D:\\wekadataset\\mll");
	static{
		Properties p = new Properties();
		try {
			p.load(Util.class.getResourceAsStream("configuration.properties"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		dir = new File(p.getProperty("DATASET_DIR"));
	}
	
	private static int NUMBER_EVALS =6;
	private static void println(Object o){
		ps.println(o);
		System.out.println(o);
	}
	private static void print(Object o){
		ps.print(o);
		System.out.print(o);
	}
	
	public static <EleType> void forEach(Consumer<EleType> consumer,EleType ... arr) throws FileNotFoundException, IOException{
		for(EleType e: arr){
			consumer.accept(e);
		}
	}
	
	public static void forEachArffFileInstances(Consumer<Instances> consumer) throws FileNotFoundException, IOException{
		File files[] = dir.listFiles((dir,name)->name.endsWith(".arff")||name.endsWith(".ARFF"));
		for(File f:files){
			consumer.accept(new Instances(new FileReader(f)));
		}
	}
	
	public static void forEachArffFileInstances2(BiFunction<Instances, Classifier, Counter> func) throws FileNotFoundException, IOException{
		
	}
	
	public static void setBaseDir(String dir){
		Util.dir = new File(dir);
	}
	public static Instances read(File arff) throws FileNotFoundException, IOException{
		return new Instances(new FileReader(arff));
	}
	public static Instances read(String arff) throws FileNotFoundException, IOException{
		return new Instances(new FileReader(arff));
	}
	
	public static Instances readFromDir(String arffName) throws FileNotFoundException, IOException{
		return new Instances(new FileReader(new File(dir,arffName)));
	}
	public static Instances readWeatherNominal() throws FileNotFoundException, IOException{
		Instances in = read(new File(dir,"weather.nominal.arff"));
		in.setClassIndex(in.numAttributes()-1);
		return in;
	}
	public static Instances readWeatherNumeric() throws FileNotFoundException, IOException{
		Instances in = read(new File(dir,"weather.numeric.arff"));
		in.setClassIndex(in.numAttributes()-1);
		return in;
	}
	
	
	public static void redirectStdOut(String filename) throws FileNotFoundException{
		String userdir = System.getProperty("user.dir");
		System.out.println("文件保存目录:"+userdir);
		if(filename == null)
			filename = "result.txt";
		System.out.println("文件名称"+filename);
		File log = new File(userdir,filename);
		System.out.println("全路径:"+log.getAbsolutePath());
		ps = new PrintStream(log);
		
	}
	private static PrintStream ps = null;
	
	 public static String arrayToTabString(double[] a) {
	        if (a == null)
	            return "null";
	        int iMax = a.length - 1;
	        if (iMax == -1)
	            return "[]";

	        StringBuilder b = new StringBuilder();
	        b.append('[');
	        for (int i = 0; ; i++) {
	            b.append(a[i]);
	            if (i == iMax)
	                return b.append(']').toString();
	            b.append("	");
	        }
	    }
	 public static void arffToCSV(File arffIn,File csvOut) throws FileNotFoundException, IOException{
		 Instances in = read(arffIn);
		 CSVSaver saver = new CSVSaver();
//		 saver.setDestination(file);
		 saver.setFile(csvOut);
		 saver.setInstances(in);
		 saver.writeBatch();
		 
		 
	 }
	 public static void main(String args[]) throws FileNotFoundException, IOException{
		 String path = "D:\\wekadataset\\timeseries\\datasets\\ARFF\\TwoLeadECGtrain.arff";
		 String out = "E:\\workspaces\\R\\tlecg.csv";
		 Util.arffToCSV(new File(path), new File(out));
		 
		 
	 }
	
}
